<?php
/*
	帮人付款
*/
defined('BY_JYA') or exit('error');
$_RQ['op']=$_RQ['op']?:'main';
if($_RQ['op']=='main'){
	$orderid=intval($_RQ['orderid']);
	$peerpay=$this->model('order/peerpay')->get_peerpay($orderid);
	$return['member']=model('core/member')->detail($peerpay['mid'],array('nickname','avatar'));	
	$pay_money=$this->pdo_getsum('order_peerpay_log',array('orderid'=>$orderid,'status'=>1),'money');
	$rate=round($pay_money*100/$peerpay['money'],2);
	$return['rate']=$rate;
	$return['need_money']=$peerpay['money']-$pay_money;
	$goods_id=$this->pdo_getcolumn('order_goods',array('orderid'=>$orderid),'goodsid');
	$return['goods']=$this->pdo_get('goods',array('id'=>$goods_id),array('id','title','image'));
	$return['goods']['image']=$return['goods']['image']?tomedia($return['goods']['image']):'';
	$return['peerpay']=$peerpay;
	if($peerpay['type']=='2'){
		//多人代付
		$logs=$this->pdo_getall('order_peerpay_log',array('orderid'=>$orderid),array(),'','id asc');
		foreach($logs as &$log){
			if($log['mid']){
				$log['member']=model('core/member')->detail($log['mid'],array('nickname','avatar'));
			}else{
				$log['member']=array('nickname'=>'匿名','avatar'=>SITEROOT.'core/resource/images/noavatar.gif');
			}
			$log['createtime']=date('Y-m-d H:i:s',$log['createtime']);
		}
		$return['logs']=$logs;
	}
	$item=array(
		'orderid'=>$orderid,
		'remark'=>'支持一下，么么哒!',
	);
	if($peerpay['type']==1){
		//单人代付
		$item['money']=$peerpay['money'];
	}
	$return['item']=$item;
	$return['sharedata']=array(
		'title'=>'我想对你说：'.$item['remark'],
		'desc'=>$item['remark'],
		'link'=>custom_url('jy_weishop/order.peerpay_pay//orderid='.$item['orderid'],true),
		'imgUrl'=>tomedia($return['goods']['image']),
	);
	set_html('title','代付详情');
	exi($return);
}
if($_RQ['op']=='post'){
	$post=get_postdata();
	$orderid=intval($post['orderid']);
	$money=round($post['money'],2);
	$this->model('order/peerpay')->get_peerpay($orderid,$money);
	$new_peerpay=array(
		'uniacid'=>$_SESSION['uniacid'],
		'orderid'=>$orderid,
		'mid'=>$_SESSION['uid'],
		'money'=>$money,
		'status'=>0,
		'createtime'=>TIMESTAMP,
		'remark'=>trim($post['remark']),
	);
	$this->pdo_insert('order_peerpay_log',$new_peerpay);
	$id=$this->pdo_insertid();
	$params=array(
		'id'=>$id,
		'title'=>'帮人代付',
		'fee'=>$new_peerpay['money'],
		'no_type'=>array('hdfk'),//可选，此项作用为指定不允许使用哪种支付方式
		'back_url'=>'jy_weishop/order.peerpay_pay/pay_result',//必填，指定异步支付结果通知地址，采用系统标准URL书写格式
	);
	$payid=cfc('pay')->create_pay_order($params);
	$this->pdo_update('order_peerpay_log',array('pay_id'=>$payid),array('id'=>$id));
	exi($payid);
}
if($_RQ['op']=='pay_result'){
	//支付结果
	$pay_result=$_SESSION['pay_result'];
	if(!$pay_result['status']){
		exi('支付失败！','error');
	}
	$peerpay_log=$this->pdo_get('order_peerpay_log',array('id'=>$pay_result['id']));
	if(!$peerpay_log || $peerpay_log['status']==1){
		exi('代付订单不存在或已完成！','error');
	}
	if($peerpay_log['money']!=$pay_result['fee']){
		exi('代付订单金额不匹配，支付失败！','error');
	}
	$this->pdo_update('order_peerpay_log',array('status'=>1),array('id'=>$peerpay_log['id']));
	$peerpay=$this->pdo_get('order_peerpay',array('orderid'=>$peerpay_log['orderid']));
	if($peerpay['type']==1){
		//类型为单人代付，修改代付订单状态为已付款
		$this->model('order/peerpay')->ispay($peerpay['orderid']);
	}else{
		//统计已完成代付金额是否足够
		$pay_money=$this->pdo_getsum('order_peerpay_log',array('orderid'=>$peerpay['orderid'],'status'=>1),'money');
		if($pay_money==$peerpay['money']){
			$this->model('order/peerpay')->ispay($peerpay['orderid']);
		}
	}
	return true;
}